package com.cw.realtime.common
package bean


import com.cw.realtime.common.util.{JdbcUtil, Queryable}
import io.circe.{Decoder, HCursor}

import java.sql.ResultSet

case class TableProcessDwd(
  sourceTable: String,
  sourceType: String,
  sinkTable: String,
  sinkColumns: String,
  op: String
)


object TableProcessDwd {
  implicit val decoder: Decoder[TableProcessDwd] = (c: HCursor) => {
    for {
      op <- c.downField("op").as[String]
      dec = Decoder
        .forProduct4("source_table", "source_type", "sink_table", "sink_columns")(TableProcessDwd(_, _, _, _, op))
      data = if ("d" == op) c.downField("before") else c.downField("after")
      tpd <- data.as[TableProcessDwd](dec)
    } yield tpd


  }

  implicit val qu: Queryable[TableProcessDwd] = (result: ResultSet) => {
    val params = (1 to 4).map(result.getString)
    TableProcessDwd(params(0), params(1), params(2), params(3), "r")
  }


  /*
  *
  *{
    "before": null,
    "after": {
      "source_table": "user_info",
      "source_type": "insert",
      "sink_table": "dwd_user_register",
      "sink_columns": "id,create_time"
    },
    "source": {
      "version": "1.9.7.Final",
      "connector": "mysql",
      "name": "mysql_binlog_source",
      "ts_ms": 0,
      "snapshot": "false",
      "db": "gmall_config",
      "sequence": null,
      "table": "table_process_dwd",
      "server_id": 0,
      "gtid": null,
      "file": "",
      "pos": 0,
      "row": 0,
      "thread": null,
      "query": null
    },
    "op": "r",
    "ts_ms": 1728439634982,
    "transaction": null
  }
  *
  *
  *
  *
  *
  *
  *
  * */

}
